# -*- coding:utf-8 -*-
from flask import Flask, request, jsonify, abort
from flask_cors import CORS
from com.zzh.dao.invoiceDao import InvoiceAPI

from . import app

CORS(app, supports_credentials=True)

'''发票管理表 zzh_invoice API'''


@app.route('/query-invoice/<int:index>', methods=['POST'])  # 查询zzh_prochild表。疑问：按照什么规则查询？？？
def queryInvoice(index):
    queryResultOfDatabaseInvoice, invoiceListJson = InvoiceAPI().queryInvoice(index)  # 返回值的获取顺序要匹配

    if queryResultOfDatabaseInvoice:
        return jsonify({"message": {"code": 1, "info": "已查询到发票信息"}, "invoiceListJson": invoiceListJson})
    else:
        return jsonify({"message": {"code": 0, "info": "未查询到发票信息"}})


@app.route('/add-invoice', methods=['POST'])  # invoice表添加记录
def addInvoice():
    if not request.json:
        print request.json
        abort(400)
    addInvoice = {
        'invoiceType': request.json.get('invoiceType', ""),  # invoiceType  发票类型
        'invoiceContent': request.json.get('invoiceContent', ""),  # invoiceContent 发票内容
        'invoiceHeader': request.json.get('invoiceHeader', ""),  # invoiceHeader  发票抬头
        'invoiceStatus': request.json.get('invoiceStatus', 0),  # invoiceStatus  出票状态，0：默认未出票。1：已出票
        'invoiceRemark': request.json.get('invoiceRemark', '')
    }
    addInvoiceResult = InvoiceAPI().addInvoice(addInvoice)
    if addInvoiceResult:
        return jsonify({"addInvoiceInfo": addInvoice,
                        "massage": {"code": 1, "info": "addInvoice OK"}})  # 添加函数调用成功，返回添加的信息（不包括prochild_id）
    else:
        return jsonify({"message": {"code": 0, "info": "addInvoice Failed"}})  # 添加函数调用失败


@app.route('/update-invoice/<int:invoiceId>', methods=['POST'])  # 根据invoiceId更新invoice表指定记录
def updateInvoice(invoiceId):
    if not request.json:
        print request.json
        abort(400)
    updateInvoice = {
        'invoiceType': request.json.get('invoiceType', ""),  # invoiceType  发票类型
        'invoiceContent': request.json.get('invoiceContent', ""),  # invoiceContent 发票内容
        'invoiceHeader': request.json.get('invoiceHeader', ""),  # invoiceHeader  发票抬头
        'invoiceStatus': request.json.get('invoiceStatus', 0),  # invoiceStatus  出票状态，0：默认未出票。1：已出票
        'invoiceRemark': request.json.get('invoiceRemark', '')
    }
    updateInvoiceResult = InvoiceAPI().updateInvoiceByInvoiceId(updateInvoice, invoiceId)
    if updateInvoiceResult:
        return jsonify({"updateInvoiceInfo": updateInvoice,
                        "massage": {"code": 1, "info": "updateInvoice OK"}})  # 添加函数调用成功，返回添加的信息（不包括prochild_id）
    else:
        return jsonify({"message": {"code": 0, "info": "updateInvoice Failed"}})  # 添加函数调用失败


@app.route('/delete-invoice/<int:invoiceId>', methods=['DELETE'])  # 根据invoiceId删除invoice表指定记录
def deleteInvoice(invoiceId):
    deleteInvoiceResult = InvoiceAPI().deleteInvoiceById(invoiceId)
    if deleteInvoiceResult:
        return jsonify({"message": {"code": 1, "info": "删除发票记录操作成功！"}})
    else:
        return jsonify({"message": {"code": 0, "info": "删除发票记录操作失败！"}})
